<?php

class m_form_actions_block_EditStructureAction extends m_backoffice_extends_Form
{
	public function execute()
	{
        $this->addToModel('isTabbed', $this->isTabbed());
        $this->addToModel('tabName', $this->getTabName('structure'));

        $document = $this->getRequestDocument();

        if($this->getTabName() == 'structure')
        {
            if($this->isSubmitted())
            {
                $formValues = $this->getFormValues('form');
                /* @var $document m_form_documents_Structure */
                $document->setStructure($formValues['structure'])->save();
            }            
        }
        else if($this->getTabName() == 'inbox')
        {
            $listService = m_backoffice_services_ListService::getInstance();

            $pagination = $listService->pagination("form", $document->getRecordsNb(), $this->getRequestParameter("numPage"), $this->getRequestParameter("nbDoc"), 20);

            $pagination['numPage'] = $pagination['numPage'] == 0 ? 1 : $pagination['numPage'];

            $offset = ($pagination['numPage']-1) * $pagination['nbDoc'];

            $this->addToModel('nbDoc', $pagination['nbDoc']);
            $this->addToModel('numPage', $pagination['numPage']);
            $this->addToModel('pagination', $pagination);
            $this->addToModel('records', $document->getRecords($pagination['nbDoc'], $offset));
        }

        $this->addToModel('form', $document);

        $this->setView('success');
	}
}